package com.ruoyi.tj.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.tj.Const;
import com.ruoyi.tj.entity.ZfZhufangtijianxinxi;
import com.ruoyi.tj.mapper.ZfZhufangtijianxinxiMapper;
import com.ruoyi.tj.service.ZfZhufangtijianxinxiService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.tj.utils.ObjectUtils;
import com.ruoyi.tj.vo.BizQueryParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;

/**
 * <p>
 * 住房-住房体检信息 服务实现类
 * </p>
 *
 * @author aoliao
 * @since 2024-03-16
 */
@Service
public class ZfZhufangtijianxinxiServiceImpl extends BaseBizServiceImpl<ZfZhufangtijianxinxiMapper, ZfZhufangtijianxinxi> implements ZfZhufangtijianxinxiService {

    @Resource
    ISysDictTypeService dictTypeService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Throwable.class)
    public void deleteByXiaoQuIds(List<String> xiaoquIds) {
        LambdaQueryWrapper<ZfZhufangtijianxinxi> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ZfZhufangtijianxinxi::getXiaoquId, xiaoquIds);
        List<ZfZhufangtijianxinxi> list = this.list(queryWrapper);
        for (ZfZhufangtijianxinxi one : list) {
            one.setDeleted(true);
        }
        this.saveOrUpdateBatch(list);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Throwable.class)
    public void deleteByLoudongIds(List<String> loudongIds) {
        LambdaQueryWrapper<ZfZhufangtijianxinxi> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ZfZhufangtijianxinxi::getLoudongId, loudongIds);
        List<ZfZhufangtijianxinxi> list = this.list(queryWrapper);
        for (ZfZhufangtijianxinxi one : list) {
            one.setDeleted(true);
        }
        this.saveOrUpdateBatch(list);
    }

    @Override
    public IPage<ZfZhufangtijianxinxi> listPageByType(String type, ZfZhufangtijianxinxi record, BizQueryParam params) {
        SysDictType dictType = new SysDictType();
        dictType.setRemark(type);
        List<SysDictType> dictTypeList = dictTypeService.selectDictTypeList(dictType);
        List<String> dictTypes = dictTypeList.stream().map(SysDictType::getDictType).collect(Collectors.toList());

        LambdaQueryWrapper<ZfZhufangtijianxinxi> queryWrapper = new LambdaQueryWrapper<>();
        ObjectUtils.setEmptyStringFieldsNull(record);
        queryWrapper.setEntity(record);
        if(dictTypes.size() > 0) {
            queryWrapper.in(ZfZhufangtijianxinxi::getDictType, dictTypes);
        }
        if(params.getBeginTime() != null) {
            queryWrapper.gt(ZfZhufangtijianxinxi::getCreateTime, params.getBeginTime());
        }
        if(params.getEndTime() != null) {
            queryWrapper.lt(ZfZhufangtijianxinxi::getCreateTime, params.getEndTime());
        }
        SysUser user = ShiroUtils.getSysUser();
        if(params.getScope() == Const.SCOPE_ALL) {
            queryWrapper.likeRight(ZfZhufangtijianxinxi::getDeptPath, user.getDeptPath());
        } else if (params.getScope() == Const.SCOPE_GROUP){
            queryWrapper.eq(ZfZhufangtijianxinxi::getDeptPath, user.getDeptPath());
        } else if(params.getScope() == Const.SCOPE_SELF) {
            queryWrapper.eq(ZfZhufangtijianxinxi::getUserId, user.getUserId());
        } else {
            queryWrapper.eq(ZfZhufangtijianxinxi::getUserId, user.getUserId());
        }

        int page = params.getPage();
        int size = params.getSize();
        page = page > 0 ? page : 1;
        size = size > 0 ? size : Const.PAGE_LIMIT;
        queryWrapper.eq(ZfZhufangtijianxinxi::getDeleted, 0);
        String sortCol = StrUtil.toUnderlineCase(params.getSortCol());
        ISqlSegment[] sqlSegments = {ORDER_BY, () -> sortCol, params.getSortType().equalsIgnoreCase("asc") ? ASC : DESC};
        queryWrapper.getExpression().add(sqlSegments);
        IPage<ZfZhufangtijianxinxi> iPage = new Page<>(page, size);
        return this.getBaseMapper().selectPage(iPage, queryWrapper);
    }
}
